/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.wsinfra.wsrm.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Calendar;

import cgl.narada.wsinfra.schemas.wsrm.AcknowledgementIntervalDocument;
import cgl.narada.wsinfra.schemas.wsrm.BaseRetransmissionIntervalDocument;
import cgl.narada.wsinfra.schemas.wsrm.ExponentialBackoffDocument;
import cgl.narada.wsinfra.schemas.wsrm.InactivityTimeoutDocument;
import cgl.narada.wsinfra.util.MarshallingUtils;
import cgl.narada.wsinfra.wsrm.WsrmSequencePolicies;
/** This interface encapsulates the WsrmPolicies associated with a sequence. 
    Among the elements that can be set in the Policy associated with a 
    Sequence are
    
    <br>(a) Sequence creation, expiration and Termination
    <br>(b) This also provides information on the retransmission interval and
    the inactivity timeout.
     
    @author Shrideep Pallickara and Sangmi Lee
    $Date: 2005/07/29 22:41:30 $
    $Revision: 1.5 $
*/

public class WsrmSequencePoliciesImpl implements WsrmSequencePolicies {
  private Calendar sequenceExpiration = null;

  private InactivityTimeoutDocument inactivityTimeout = null;
  private BaseRetransmissionIntervalDocument retransmissionInterval = null;
  private ExponentialBackoffDocument exponentialBackoff = null;
  private AcknowledgementIntervalDocument acknowledgementInterval = null;
  private MarshallingUtils marshallingUtils = MarshallingUtils.getInstance();
  private String moduleName = "WsrmSequencePolicies: ";
  
  public 
  WsrmSequencePoliciesImpl(Calendar sequenceExpiration,
			   InactivityTimeoutDocument inactivityTimeout,
			   BaseRetransmissionIntervalDocument retransInterval,
			   ExponentialBackoffDocument exponentialBackoff,
			   AcknowledgementIntervalDocument ackInterval) {
    this.sequenceExpiration = sequenceExpiration;
    this.inactivityTimeout = inactivityTimeout;
    this.retransmissionInterval = retransInterval;
    this.exponentialBackoff = exponentialBackoff;
    this.acknowledgementInterval = ackInterval;    
  }


  /** Check to see if sequence expiration has been specified */
  public boolean hasSequenceExpiration() {
    if (sequenceExpiration != null) {
      return true;
    }
    return false;
  }


  /** Retrieve the specified sequence expiration */
  public Calendar getSequenceExpiration() {
    return sequenceExpiration;
  }

  protected void resetSequenceExpiration(Calendar sequenceExpiration) {
    this.sequenceExpiration = sequenceExpiration;
  }

  /** Check to see if an inactivity timeout has been specified */
  public boolean hasInactivityTimeout() {
    if (inactivityTimeout != null) {
      return true;
    }
    return false;
  }

  /** Retrieve specified inactivity timeout info */
  public InactivityTimeoutDocument getInactivityTimeout() {
    return inactivityTimeout;
  }

  protected void 
  setInactivityTimeout(InactivityTimeoutDocument inactivityTimeout) {
    this.inactivityTimeout = inactivityTimeout;
  }


  /** Check if retransmission related policies have been specified */
  public boolean hasRetransmissionInterval() {
    if (retransmissionInterval != null) {
      return true;
    }
    return false;
  }


    
  /** Retrieve the retransmission interval related policies */
  public BaseRetransmissionIntervalDocument getRetransmissionInterval() {
    return retransmissionInterval;
  }

  protected void 
  setRetransmissionInterval(BaseRetransmissionIntervalDocument 
			    retransmissionInterval) {
    this.retransmissionInterval = retransmissionInterval;
  }
  

  /* Check if the exponential back off has been specified */
  public boolean hasExponentialBackoff() {
    if (exponentialBackoff != null) {
      return true;
    }
    return false;
  }
  

  /** Retrieve the specified exponential back off */
  public ExponentialBackoffDocument getExponentialBackoff() {
    return exponentialBackoff;
  }


  protected void 
  setExponentialBackoff(ExponentialBackoffDocument exponentialBackoff) {
    this.exponentialBackoff = exponentialBackoff;
  }


  /** Check if an acknowledgement interval has been specified */
  public boolean hasAcknowledgementInterval() {
    if (acknowledgementInterval != null) {
      return true;
    }
    return false;
  }

  /* Retrieve the specified acknowldgement interval */
  public AcknowledgementIntervalDocument getAcknowledgementInterval() {
    return acknowledgementInterval;
  }


  protected void 
  setAcknowledgementInterval(AcknowledgementIntervalDocument acknowledgementInterval) {
    this.acknowledgementInterval = acknowledgementInterval;
  }


  /** Get the byte stream representation */
  public byte[] getBytes() {
    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(baOutputStream);
    byte[] marshalledBytes= null;
    try {
      marshallingUtils.writeObjectToStream(sequenceExpiration, dout);
      marshallingUtils.writeObjectToStream(inactivityTimeout, dout);
      marshallingUtils.writeObjectToStream(retransmissionInterval, dout);
      marshallingUtils.writeObjectToStream(exponentialBackoff, dout);
      marshallingUtils.writeObjectToStream(acknowledgementInterval, dout); 

      marshalledBytes = baOutputStream.toByteArray();
    } catch (Exception ioe) {
      System.out.println(moduleName + "Error while marshalling" + ioe);
    } /** end try-catch block*/
    
    return marshalledBytes;
  }

  
  public WsrmSequencePoliciesImpl(byte[] marshalledBytes) {
    ByteArrayInputStream baInputStream = 
      new ByteArrayInputStream(marshalledBytes);
    DataInputStream din = new DataInputStream(baInputStream);

    try {
      Object sequenceExpirationObj, inactivityTimeoutObj, retransmissionIntervalObj, exponentialBackoffObj, acknowledgementIntervalObj;
      
      sequenceExpirationObj = marshallingUtils.readObjectFromStream(din);
      if (sequenceExpirationObj != null) {
	sequenceExpiration = (Calendar) sequenceExpirationObj;
      }


      inactivityTimeoutObj = marshallingUtils.readObjectFromStream(din);
      if (inactivityTimeoutObj != null) {
	inactivityTimeout = (InactivityTimeoutDocument) inactivityTimeoutObj;
      }

      retransmissionIntervalObj = marshallingUtils.readObjectFromStream(din);
      if (retransmissionIntervalObj != null) {
	retransmissionInterval = 
	  (BaseRetransmissionIntervalDocument)retransmissionIntervalObj;
      }

      exponentialBackoffObj = marshallingUtils.readObjectFromStream(din);
      if (exponentialBackoffObj != null) {
	exponentialBackoff = (ExponentialBackoffDocument)exponentialBackoffObj;
      }

      acknowledgementIntervalObj = marshallingUtils.readObjectFromStream(din);
      if (acknowledgementIntervalObj != null) {
	acknowledgementInterval = 
	  (AcknowledgementIntervalDocument) acknowledgementIntervalObj;
      }
      
    } catch (Exception ioe) {
      System.out.println(moduleName + "Error while un-marshalling" + ioe);
      ioe.printStackTrace();
    }/** end try-catch block*/
  
  }
  
}
